package cn.user.domain.dao;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import cn.user.domain.Favorite;

public class FavoriteDao {
	private JdbcTemplate jdbctemplate = new JdbcTemplate(Druid0.getDataSource());
	
	public void addColl(int foodid,String img,int uid) {
		String sql = "INSERT INTO favorite VALUES(?,?,?)";
		jdbctemplate.update(sql,foodid,img,uid);
	}
	
	public boolean isExist(int foodid,int uid)
	{
		try{
			String sql = "select * from favorite where foodid = ? and uid = ?";
			Favorite favor = jdbctemplate.queryForObject(sql, new BeanPropertyRowMapper<Favorite>(Favorite.class),foodid,uid);
			return true;
			}
			catch(DataAccessException e) {
				//e.printStackTrace();
				return false;
			}
	}
	
	public List<Favorite> findOne(int uid)
	{
		try{
			String sql = "select * from favorite where uid = ?";
			return jdbctemplate.query(sql, new BeanPropertyRowMapper<Favorite>(Favorite.class),uid);
			}
			catch(DataAccessException e) {
				e.printStackTrace();
				return null;
			}
	}
	
	public void delColl (int foodid) {
		String sql = "delete from favorite where foodid = ?";
		jdbctemplate.update(sql,foodid);
	}
	
}
